<?php
class Talktangle_Api_Ptangle extends Core_Api_Abstract
{
    public function getNumberOffsetImage(){
        $settingApi = Engine_Api::_()->getApi('settings','core');
        $colCount = $settingApi->getSetting('talktanlge.pic.offsetimage',5);//15;
        return $colCount;
    }
    
    public function checkNotFinish($matrix){
        $row = $col = $this->getNumberOffsetImage();
        $index = 1;
        
        for($i = 1; $i <= $row; $i++) {
            for($j = 1; $j <= $row; $j++) {
                if ($matrix[$i][$j] != $index)  return true;
                $index++;
            }
        }
        
        return false;
    }
    
    public function checkPlayerFinish($matrix,$steps){
        
    }
    
    public  function initMatrix(){
        
        return $this->generateMatrix();
        
        $matrix = array();
        $row = $col = $this->getNumberOffsetImage() +2;
        for( $i =0; $i < $row; $i++) {
            $matrix[0][$i] = -1;
            $matrix[$row-1][$i] = -1;
            $matrix[$i][0] = -1;
            $matrix[$i][$row-1] = -1;
        };
        $matrix[0][1]=0;
        $max = ($row-2)*($row-2);
        $row = $row -2;
        
        $a = array();
        $result = array();
        $index = 1;
        while($index < $max){
            $q =  mt_rand(2,$max);
            if(!isset($a[$q])) {
                $result[$index] = $q;
                $a[$q] = $q;
                $index++;
            }
        }
        $result[0] =1;
        
        //for($i =0;$i <$max ;$i++) $result[$i] = $i+1;
        $index = 0;
        for ( $i = 1; $i <= $row; $i++ ) {
            for ( $j = 1; $j <= $row; $j++ ) {
                $matrix [$i][$j] = $result[$index];
                $index++;
            }
        }
        return $matrix;
       
    }
    public function unPictangle($steps){
        $pic_session = new Zend_Session_Namespace('pic_session');
        $matrix = $pic_session->matrix;
        //echo "<pre>".print_r($steps,true)."</pre>";
        $stepCount = count($steps);
        for ( $i = 0; $i < $stepCount; $i ++ ) {
            $ok = $this->checkVaildStep($matrix,$steps[$i]);
            if (!$ok) {
                return false;
            }
            
            
        }
        $ok = !$this->checkNotFinish($matrix);
        if(!$ok) return false;
        return true;
        
    }
    
    
    private function checkVaildStep(&$matrix,$step){
        $currentPosition = $step['old'];
        $direction = $step['direction'];
        
        $supportDirection = array(
            1,3,4,2
        );
        if (!in_array($direction,$supportDirection)) return false;
        $nextPosition = $currentPosition;
        switch ($direction) {
            case 1: //top
                    $nextPosition['row'] -= 1; 
                break;
            case 4: //rihg
                    $nextPosition['col'] += 1; 
                break;
            case 2: //bottom
                    $nextPosition['row'] += 1; 
                break;
            case 3: //left
                    $nextPosition['col'] -= 1; 
                break;
        }
        
       if (!empty($matrix[$nextPosition['row']][$nextPosition['col']]))  return false;
            
        $matrix[$nextPosition['row']][$nextPosition['col']] = $matrix[$currentPosition['row']][$currentPosition['col']];
        $matrix[$currentPosition['row']][$currentPosition['col']] = 0;
        
        return true;
        
        
        
    }
    
    public function getAllOffsetOfTangle($pic){
        $table = Engine_Api::_()->getDbtable('files','storage');
        $select = $table->select()
                        ->where('parent_type = ?',$pic->getType())
                        ->where('parent_id = ?',$pic->photo_id);
        
        $rows = $table->fetchAll($select);
        return $rows;
        
    }
    
    public function checkFinishMatrix($matrix){
        $max = $this->getNumberOffsetImage();
        $index = 1;
        for($i = 0 ; $i < $max; $i++){
            for($j =0 ; $j < $max ; $j++){
                if($matrix[$i][$j] == $index){
                    $index ++;
                } else {
                    return false;
                }
            }
        }
        
        return true;
        
    }
    
    public function generateMatrix($id = 0){
        $matrix = array();
        $settingApi = Engine_Api::_()->getApi('settings','core');
        $row = $col = $this->getNumberOffsetImage() +2;
        for( $i =0; $i < $row; $i++) {
            $matrix[-1][$i] = -1;
            $matrix[0][$i] = -1;
            $matrix[$row-1][$i] = -1;
            $matrix[$i][0] = -1;
            $matrix[$i][$row-1] = -1;
        };
        
        
        $index = 1;
        for( $i = 1; $i < $row - 1; $i++){
            for( $j = 1; $j < $row - 1; $j++){
                $matrix[$i][$j] = $index;
                $index ++; 
            }
        }
        $matrix[0][1]=0;
        
        
        $x = 1;
        $y = 1;
        
        $playStep = array();
        $colCount = $settingApi->getSetting('talktanlge.pic.step',($row + 1 )*($row + 1));
        for($i = 0 ; $i < $colCount ; $i ++ ){
            $direction = $this->swap($matrix,$x,$y);
            
            $value = $matrix[$x][$y];
            
            if ($direction == 1){
                $t = $matrix[$x][$y];
                $matrix[$x][$y] = 0;
                $matrix[$x-1][$y] = $t;
                
                $playStep[] = array(
                    'old' => array('x'=>$x,'y' => $y),
                    'new' => array('x'=>$x-1,'y' => $y)
                );
            }
            if ($direction == 2){
                $t = $matrix[$x][$y];
                $matrix[$x][$y] = 0;
                $matrix[$x][$y-1] = $t;
                
                $playStep[] = array(
                    'old' => array('x'=>$x,'y' => $y),
                    'new' => array('x'=>$x,'y' => $y -1)
                );
            }
            if ($direction == 3){
                $t = $matrix[$x][$y];
                $matrix[$x][$y] = 0;
                $matrix[$x+1][$y] = $t;
                
                $playStep[] = array(
                    'old' => array('x'=>$x ,'y' => $y ),
                    'new' => array('x'=>$x+1,'y' => $y)
                );
            }
            if ($direction == 4){
                $t = $matrix[$x][$y];
                $matrix[$x][$y] = 0;
                $matrix[$x][$y+1] = $t;
                
                $playStep[] = array(
                    'old' => array('x'=>$x,'y' => $y),
                    'new' => array('x'=>$x,'y' => $y+1)
                );
            }
            
            
            $d = $this->findDirection($matrix,$x,$y,$value);
            if ($d == 1){
                $x = $x - 1;
            }
            if ($d == 2){
                $y = $y - 1;
            }
            if ($d == 3){
                $x = $x + 1 ;
            }
            if ($d == 4){
               $y = $y + 1;
            }
            
        }
        unset($matrix[-1]);
        $pic_session = new Zend_Session_Namespace('pic_session');
        $pic_session->stepGenerate = $playStep;
    
        return $matrix;
    }
    
    
    public function findDirection($maxtrix,$x,$y,$value){
        $direction = array(); // 1 top 2 left 3 down 4 righ;
        if ($x < 0 || $y < 0 ) return 0;
        if ($maxtrix[$x-1][$y] != - 1 && $maxtrix[$x-1][$y] != $value ) $direction[] = 1;
        if ($maxtrix[$x][$y-1] != - 1 && $maxtrix[$x][$y-1] != $value ) $direction[] = 2;
        if ($maxtrix[$x+1][$y] != - 1 && $maxtrix[$x+1][$y] != $value ) $direction[] = 3;
        if ($maxtrix[$x][$y+1] != - 1 && $maxtrix[$x][$y+1] != $value ) $direction[] = 4;
       
        $max = count($direction);
        
        if($max <= 0)  return 0;
        $q =  mt_rand(0,$max -1);
         
        return $direction[$q];
    }
    
    public function swap($maxtrix,$x,$y){
        if ($x < 0 || $y < 0 ) return 0;
        $direction = array(); // 1 top 2 left 3 down 4 righ;
        if (empty($maxtrix[$x-1][$y])) $direction[] = 1;
        if (empty($maxtrix[$x][$y-1])) $direction[] = 2;
        if (empty($maxtrix[$x+1][$y])) $direction[] = 3;
        if (empty($maxtrix[$x][$y+1])) $direction[] = 4;
        
        $max = count($direction);
        if($max <= 0)  return 0;
        $q =  mt_rand(0,$max -1);
        
        return $direction[$q];
    }
    
    public function suggest($stepIndex = 0){
        $pic_session = new Zend_Session_Namespace('pic_session');
        $matrix = $pic_session->stepGenerate;
        $length = count($matrix);
        $step = $matrix[$length - 1 -$stepIndex];
        return $step;
    }
    
    
}
?>